package pl.edu.pjwstk.kor.database.init;

import java.util.ArrayList;
import java.util.List;

import pl.edu.pjwstk.kor.model.CustomDate;
import pl.edu.pjwstk.kor.model.Customer;
import pl.edu.pjwstk.kor.model.Deal;
import pl.edu.pjwstk.kor.model.Employee;
import pl.edu.pjwstk.kor.model.Service;

public class DealInitalizer extends AbstractDataInitalizer<Deal> {
	private final List<Customer> customers;
	private final List<Employee> employees;
	private final List<Service> services;
	
	public DealInitalizer(List<Customer> customers, List<Employee> employees, List<Service> services) {
		this.customers = customers;
		this.employees = employees;
		this.services = services;
	}

	@Override
	public List<Deal> initData() throws Exception {
		List<Deal> result = new ArrayList<Deal>();
		for(Customer c : customers) {
			for(int i = 0 ; i < random.nextInt(9) + 1; i++) {
				Deal d = new Deal();
				d.setCustomer(c);
				c.getDeals().add(d);
				
				Employee e = selectRandom(employees, random);
				d.setEmployee(e);
				e.getDeals().add(d);
				
				d.setService(selectRandom(services, random));
				d.setDealDate(new CustomDate(random.nextInt(12)+2000, random.nextInt(11) + 1, random.nextInt(27) + 1, random.nextInt(23), random.nextInt(60)));
				double delta = d.getService().getMaxServiceCost() - d.getService().getMinServiceCost();
				double cost = d.getService().getMinServiceCost() + random.nextInt((int)delta);
				d.setCost(cost);
				result.add(d);
			}
		}
		
		return result;
	}

}
